package com.willy.ratingbar;

import ohos.agp.components.AttrSet;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.utils.LayoutAlignment;
import ohos.app.Context;
import ohos.global.resource.RawFileEntry;
import ohos.global.resource.ResourceManager;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;

import static ohos.agp.components.DependentLayout.LayoutConfig.CENTER_IN_PARENT;

/**
 * Created by willy
 * on 2017/6/3.
 */
class PartialView extends DependentLayout {

    private Image mFilledView;
    private Image mEmptyView;
    private Image mHalfView;
    private int mStarWidth = 100;
    private int mStarHeight = 100;

    public PartialView(Context context, int partialViewId, int starWidth, int startHeight,
                       int padding) {
        super(context);
        mStarWidth = starWidth;
        mStarHeight = startHeight;
        setTag(partialViewId);
        setPadding(padding, padding, padding, padding);
        init();
    }

    public PartialView(Context context, AttrSet attrs) {
        super(context, attrs);
        init();
    }

    public PartialView(Context context, AttrSet attrs, String defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setLayoutConfig(new DirectionalLayout.LayoutConfig(
                LayoutConfig.MATCH_CONTENT,
                LayoutConfig.MATCH_CONTENT,
                LayoutAlignment.CENTER,
                1f));

        DependentLayout.LayoutConfig params = new DependentLayout.LayoutConfig(
                mStarWidth == 0 ? LayoutConfig.MATCH_CONTENT : mStarWidth,
                mStarHeight == 0 ? LayoutConfig.MATCH_CONTENT : mStarHeight);
        params.addRule(CENTER_IN_PARENT);

        mFilledView = new Image(getContext());
        mFilledView.setScaleMode(Image.ScaleMode.STRETCH);
        mFilledView.setPixelMap(getPixelMap("resources/rawfile/filled.png"));
        addComponent(mFilledView, params);
        mEmptyView = new Image(getContext());
        mEmptyView.setScaleMode(Image.ScaleMode.STRETCH);
        mEmptyView.setPixelMap(getPixelMap("resources/rawfile/empty.png"));
        addComponent(mEmptyView, params);
        mHalfView = new Image(getContext());
        mHalfView.setScaleMode(Image.ScaleMode.STRETCH);
        mHalfView.setPixelMap(getPixelMap("resources/rawfile/half.png"));
        addComponent(mHalfView, params);
        setEmpty();
    }

    private PixelMap getPixelMap(String rawfile) {
        ResourceManager resManager = getResourceManager();
        RawFileEntry rawFileEntry = resManager.getRawFileEntry(rawfile);
        try {
            InputStream resource = rawFileEntry.openRawFile();
            ImageSource imageSource = ImageSource.create(resource, null);
            return imageSource.createPixelmap(null);
        } catch (IOException e) {
            System.out.println(e);
        }
        return null;
    }

    public void setFilledDrawable(PixelMap drawable) {
        if (drawable == null) {
            return;
        }
        mFilledView.setPixelMap(drawable);
    }

    public void setEmptyDrawable(PixelMap drawable) {
        if (drawable == null) {
            return;
        }
        mEmptyView.setPixelMap(drawable);
    }

    public void setFilled() {
        mFilledView.setVisibility(VISIBLE);
        mEmptyView.setVisibility(HIDE);
        mHalfView.setVisibility(HIDE);
    }

    public void setPartialFilled(float rating) {
        BigDecimal decimal1 = new BigDecimal(rating);
        BigDecimal decimal2 = new BigDecimal(1);
        BigDecimal[] decimal3 = decimal1.divideAndRemainder(decimal2);
        float percentage = decimal3[1].floatValue();
        int level = (int) (10000 * percentage);
        level = level == 0 ? 10000 : level;
        if (level > 5000) {
            mFilledView.setVisibility(VISIBLE);
            mEmptyView.setVisibility(HIDE);
            mHalfView.setVisibility(HIDE);
        } else {
            mFilledView.setVisibility(HIDE);
            mEmptyView.setVisibility(HIDE);
            mHalfView.setVisibility(VISIBLE);
        }
    }

    public void setEmpty() {
        mFilledView.setVisibility(HIDE);
        mHalfView.setVisibility(HIDE);
        mEmptyView.setVisibility(VISIBLE);
    }

    public void setStarWidth(int starWidth) {
        mStarWidth = starWidth;
        ComponentContainer.LayoutConfig params = mFilledView.getLayoutConfig();
        params.width = mStarWidth;
        mFilledView.setLayoutConfig(params);
        mEmptyView.setLayoutConfig(params);
    }

    public void setStarHeight(int starHeight) {
        mStarHeight = starHeight;
        ComponentContainer.LayoutConfig params = mFilledView.getLayoutConfig();
        params.height = mStarHeight;
        mFilledView.setLayoutConfig(params);
        mEmptyView.setLayoutConfig(params);
    }
}